<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 11:06
 */
namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\Newscat;
use think\Db;

class Newscategory extends Admin
{
    //新闻分类列表
    public function  category_list(){
            // 读取数据
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder();
            // 数据列表
            $data_list = Newscat::where($map)->order($order)->paginate();
            // 分页数据
            $page = $data_list->render();
            $btn_access1 = [
                'title' => '分类编辑',
                'icon'  => 'fa fa-fw fa-pencil',
                'class' => 'btn btn-xs btn-default',
                'href'  => url('edit', ['id' => '__id__']),
            ];
            $btn_access = [
                'title' => '新增分类',
                'icon'  => 'fa fa-fw fa-plus',
                'class' => 'btn btn-info',
                'href'  => url('add'),
            ];
            $btn_access2 = [
                'title' => '删除分类',
                'icon'  => 'fa fa-fw fa-trash-o',
                'class' => 'btn btn-xs btn-default ajax-get confirm',
                'href'  => url('delete', ['id' => '__id__']),
            ];
            //$news_category=Db::name('news_category')->column('id,name');
            $news_category['0'] = '顶级分类';
            //设置页面
            return ZBuilder::make('table')
                ->hideCheckbox()
                ->addColumn('id', '编号', 'text')
                ->addColumn('name', '分类名称', 'text.edit')
                ->addColumn('pid','上级分类','status','',$news_category)
                ->addColumn('right_button','操作','btn')
                ->setSearchArea([
                    ['text:2', 'name', '分类名称', 'like'],
                ])
                ->setTableName('news_category') // 指定数据表名
                ->addTopButton('custom',$btn_access) // 添加顶部按钮
                ->addRightButton('edit',$btn_access1) // 添加右侧按钮
                ->addRightButton('delete',$btn_access2) // 添加右侧按钮
                ->replaceRightButton(['type' => ['neq', '2']], '<button class="btn btn-danger btn-xs" type="button" disabled>不可操作</button>', ['delete'])
                ->setPrimaryKey('id') // 设置主键名为username
                ->setPageTitle('新闻分类列表')
                ->setRowList($data_list) // 设置表格数据
                ->setPages($page) // 设置分页数据
                ->fetch();
    }
    //添加新闻
    public function add(){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            //var_dump($data);die;
            $result = $this->validate($data, 'NewsCategory');
            if(true !== $result) $this->error($result);
            if ($page = Newscat::create($data)) {
                $this->success('新增成功', 'category_list');
            } else {
                $this->error('新增失败');
            }
        }else
        {
            //$news_category=Db::name('qt_news_category')->column('id,name');
            $news_category['0'] = '顶级分类';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7','name','分类名称'],
                        ['select:7','pid','选择分类','请选择所属分类',$news_category],
                        ['radio:','type','分类属性','<code>系统分类不允许删除</code>',['2'=>'新闻','1'=>'系统'],'2'],
                    ]
                )
                ->setPageTitle('添加新闻')
                ->fetch();
        }
    }



    //编辑公告
    public function edit($id=null){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'NewsCategory');
            if(true !== $result) $this->error($result);

            if (Newscat::update($data)) {
                $this->success('编辑成功', 'category_list');
            } else {
                $this->error('编辑失败');
            }
        }else
        {
            $info = Newscat::get($id);
            //$news_category=Db::name('news_category')->column('id,name');
            $news_category['0'] = '顶级分类';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden','id','id'],
                        ['text:7','name','标题'],
                        ['select:7','pid','选择上级分类','选择上级分类',$news_category],
                        ['radio', 'type', '状态', '', [1=>'系统分类', 2=>'普通分类']]
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑新闻分类')
                ->fetch();
        }
    }

    //删除新闻分类
    public function delete($id=null){
        if ($id === null) $this->error('缺少参数');
        $son=Db::name('qt_news_category')->where('pid',$id)->count();
        if($son>0){
            $this->error('请先删除下属分类','category_list');
        }
        $new=Db::name('qt_news')->where('category_id',$id)->count();
        if($new>0){
            $this->error('请先删除分类下属文章','category_list');
        }
        $type  = Db::name('qt_news_category')->where('id',$id)->vlaue('type');
        if($type == '1'){
            $this->error('该分类为系统分类不允许删除','category_list');
        }
        $re=Newscat::destroy($id);// 删除主键为1,2,3的数据
        if($re){
            $this->success('删除成功','category_list');
        }else{
            $this->error('系统繁忙请稍后再试','category_list');
        }

    }

}